package com.tstudy.laoshibang.community;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.emojicon.EmojiconTextView;
import com.tstudy.laoshibang.login.LoginActivity;
import com.tstudy.laoshibang.mode.Community;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 吐槽页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class CommunityListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<Community> mItems;
	public final int TYPE_TOP = 1;
	public final int TYPE_ITEM = 2;
	
	public int spHeight = CommonUtil.dip2px(1);
	public int mTopCount;
	public CommunityListAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	int redTextColor = BaseApplication.getResColor(R.color.theme_red);
	int grayTextColor = BaseApplication.getResColor(R.color.text_color_gray);
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<Community> items) {
		setData(items,true);
	}
	public void setData(List<Community> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<Community> getData() {
		return mItems;
	}
	
	@Override
	public int getViewTypeCount() {
		return 3;
	}
	@Override
	public int getItemViewType(int position) {
		return TextUtils.isEmpty(getItem(position).title)?TYPE_ITEM:TYPE_TOP;
	}

	@Override
	public Community getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final TopViewHolder topViewHolder;
		switch (getItemViewType(position)) {
		case TYPE_TOP:
			if(convertView ==  null){
				topViewHolder = new TopViewHolder();
				convertView = mInflater.inflate(R.layout.community_top_list_item, null);
				topViewHolder.title = (TextView) convertView.findViewById(R.id.community_top_list_item_title);
				topViewHolder.sp = (TextView) convertView.findViewById(R.id.community_top_list_item_sp);
				topViewHolder.sp1 = (TextView) convertView.findViewById(R.id.community_top_list_item_sp1);
				convertView.setTag(topViewHolder);
			}else{
				topViewHolder = (TopViewHolder) convertView.getTag();
			}
			if(topViewHolder != null){
				setTopConvertView(topViewHolder,position,convertView);
			}
			break;
		case TYPE_ITEM:
			if(convertView ==  null){
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.community_list_item, null);
				viewHolder.userAvatar = (ImageView)convertView.findViewById(R.id.community_list_item_user_avatar);
				viewHolder.userName = (TextView)convertView.findViewById(R.id.community_list_item_user_name);
				viewHolder.createTime = (TextView)convertView.findViewById(R.id.community_list_item_create_time);
				viewHolder.content = (EmojiconTextView) convertView.findViewById(R.id.community_list_item_content);
				viewHolder.imgLayout = (RelativeLayout) convertView.findViewById(R.id.community_list_item_img_layout);
				viewHolder.commentTxt = (TextView) convertView.findViewById(R.id.community_list_item_action_comment_txt);
				viewHolder.loveTxt = (TextView) convertView.findViewById(R.id.community_list_item_action_love_txt);
				viewHolder.shareTxt = (TextView) convertView.findViewById(R.id.community_list_item_action_share_txt);
				viewHolder.commentIcon = (ImageView) convertView.findViewById(R.id.community_list_item_action_comment_icon);
				viewHolder.loveIcon = (ImageView) convertView.findViewById(R.id.community_list_item_action_love_icon);
				viewHolder.shareIcon = (ImageView) convertView.findViewById(R.id.community_list_item_action_share_icon);
				viewHolder.commentLayout = (RelativeLayout) convertView.findViewById(R.id.community_list_item_action_comment_click);
				viewHolder.loveLayout = (RelativeLayout) convertView.findViewById(R.id.community_list_item_action_love_click);
				viewHolder.shareLayout = (RelativeLayout) convertView.findViewById(R.id.community_list_item_action_share_click);
				viewHolder.address = (TextView) convertView.findViewById(R.id.community_list_item_address);
				viewHolder.deleteAction =  (TextView) convertView.findViewById(R.id.community_list_item_delete);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			if(viewHolder != null){
				setConvertView(viewHolder,position,convertView);
			}
			break;

		default:
			break;
		}
		return convertView;
	}
	public void setTopConvertView(final TopViewHolder topViewHolder,final int position,View view){
		final Community community = (Community) getItem(position);
		if (position == mTopCount -1) {
			topViewHolder.sp.setVisibility(View.VISIBLE);
//			topViewHolder.sp1.setVisibility(View.VISIBLE);
			topViewHolder.sp1.getLayoutParams().height = 1;
		}else{
			topViewHolder.sp.setVisibility(View.GONE);
//			topViewHolder.sp1.setVisibility(View.GONE);
			topViewHolder.sp1.getLayoutParams().height = spHeight;
		}
		topViewHolder.title.setText(community.title);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAdapterCallBack != null) {
					mAdapterCallBack.onCommunityClick(community.tcId, position);
				}
			}
		});
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final Community community = (Community) getItem(position);
		if (TextUtils.isEmpty(community.content)) {
			viewHolder.content.setVisibility(View.GONE);
		}else{
			viewHolder.content.setText(community.content);
			viewHolder.content.setVisibility(View.VISIBLE);
		}
		viewHolder.commentTxt.setText(community.evalNum + "");
		if (community.isPraise == CONSTANT.IS_PRISED) {
			viewHolder.loveTxt.setTextColor(redTextColor);
		}else{
			viewHolder.loveTxt.setTextColor(grayTextColor);
		}
		viewHolder.loveTxt.setText(community.praiseNum + "");
		if (TextUtils.isEmpty(community.provinceName) || TextUtils.isEmpty(community.cityName)) {
			viewHolder.address.setVisibility(View.GONE);
		}else{
			viewHolder.address.setVisibility(View.VISIBLE);
			viewHolder.address.setText(community.provinceName + " " + community.cityName);
		}
		if (!TextUtils.isEmpty(community.createTime)) {
			viewHolder.createTime.setText(CommonUtil.timelineDateStr(Long.parseLong(community.createTime)));
		}
		viewHolder.loveIcon.setSelected(community.isPraise==1);
		if (community.isAnous.key == 1) {
			viewHolder.userName.setText(community.isAnous.text);
			viewHolder.userAvatar.setImageResource(R.drawable.anonymous_head);
			viewHolder.userAvatar.setOnClickListener(null);
		}else{
			viewHolder.userName.setText(community.user.nick);
			viewHolder.userAvatar.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mAdapterCallBack != null) {
						mAdapterCallBack.onAvatorClick(community.user.userNo);
					}
					
				}
			});
			
			HttpManager.getInstance().loadEditAvatar(viewHolder.userAvatar,community.user.imgIdxName+CONSTANT.AVATAR_URL ,R.drawable.profile_avatar_default);
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAdapterCallBack != null) {
					mAdapterCallBack.onItemClick(community.tcId, position);
				}
			}
		});
		viewHolder.commentLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAdapterCallBack != null) {
					mAdapterCallBack.onCommunityClick(community.tcId, position);
				}
			}
		});
		viewHolder.loveLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (BaseApplication.checkLogin()) {
					if (mAdapterCallBack != null) {
						community.isPraise = community.isPraise==1?0:1;
						viewHolder.loveIcon.setSelected(community.isPraise == 1);
						community.praiseNum = community.isPraise == 1?community.praiseNum+1:community.praiseNum-1;
						if (community.isPraise == CONSTANT.IS_PRISED) {
							viewHolder.loveTxt.setTextColor(redTextColor);
						}else{
							viewHolder.loveTxt.setTextColor(grayTextColor);
						}
						viewHolder.loveTxt.setText(community.praiseNum+"");
						CommonUtil.loveAnimator(viewHolder.loveIcon);
						mAdapterCallBack.onLoveClick(community, position,viewHolder.loveTxt);
					}
				}else{
					LoginActivity.show();
				}
			}
		});
		viewHolder.shareLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mAdapterCallBack != null) {
					mAdapterCallBack.onShareClick(community.tcId, position);
				}
			}
		});
		if (community.isDel==1) {
			viewHolder.deleteAction.setVisibility(View.VISIBLE);
			viewHolder.deleteAction.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (mAdapterCallBack != null) {
						mAdapterCallBack.onDeleteClick(community.tcId, position);
					}
				}
			});
		}else{
			viewHolder.deleteAction.setVisibility(View.GONE);
			viewHolder.deleteAction.setOnClickListener(null);
		}
//		if (community.idxes != null && community.idxes.size()>0) {
			fillImgLayout(viewHolder.imgLayout, community,position);
//		}
	}
	
	final int COLUMN_SIZE = 3;
	int photoMargin = CommonUtil.dip2px(5);
	int layoutMargin = CommonUtil.dip2px(60);
	int mBigImgWidth = BaseApplication.mScreenWidth - layoutMargin;
	int mBigImgHeigh = mBigImgWidth*2/3;
	int  mImageWidth = (BaseApplication.mScreenWidth -photoMargin*4-layoutMargin)/COLUMN_SIZE;
	int mImageHeight = mImageWidth;
	public void fillImgLayout(final RelativeLayout layout,final Community community,int position) {
		layout.removeAllViews();
		int size = community.idxes.size();
		int displaySize = Math.min(9, size);
		for (int i = 0; i < displaySize; i++) {
			ImageView imgView = new ImageView(BaseApplication.mContext);
			imgView.setId(i + 1);
			RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(mImageWidth, mImageHeight);
			if (displaySize ==1) {
				imgLp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
				imgLp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
				imgView.setScaleType(ScaleType.FIT_START);
				imgView.setAdjustViewBounds(true);
			}else{
				imgView.setScaleType(ScaleType.CENTER_CROP);
			}
			int rowIndex = i/COLUMN_SIZE;
			int columnIndex = i%COLUMN_SIZE;
			if (rowIndex != 0) {
				imgLp.addRule(RelativeLayout.BELOW,i-COLUMN_SIZE+1);
			}
			if (columnIndex != 0) {
				imgLp.setMargins(0, 0,photoMargin, photoMargin);
				imgLp.addRule(RelativeLayout.RIGHT_OF, i);
			}else{
				imgLp.setMargins(photoMargin, 0, photoMargin, photoMargin);
			}
			layout.addView(imgView, imgLp);
			layout.getLayoutParams().height = RelativeLayout.LayoutParams.WRAP_CONTENT;
			if (displaySize ==1) {
				HttpManager.getInstance().loadCommonImage(imgView,community.idxes.get(i).imgIdxName);
			}else{
				HttpManager.getInstance().loadCommonImage(imgView,community.idxes.get(i).imgIdxName + CommonUtil.buildCropUrl(imgLp.width, imgLp.height));
			}
			final int picIndex = i;
			imgView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (mAdapterCallBack != null) {
						mAdapterCallBack.onPicClick(community,picIndex);
					}
				}
			});
		}
	}
	
	static class ViewHolder{
		ImageView userAvatar;
		TextView userName;
		TextView createTime;
		TextView title;
		TextView address;
		EmojiconTextView content;
		TextView loveTxt;
		TextView commentTxt;
		TextView shareTxt;
		ImageView commentIcon;
		ImageView loveIcon;
		ImageView shareIcon;
		RelativeLayout commentLayout;
		RelativeLayout loveLayout;
		RelativeLayout shareLayout;
		TextView deleteAction;
		RelativeLayout imgLayout;
	}
	static class TopViewHolder{
		TextView title;
		TextView sp;
		TextView sp1;
	}
	
	public interface AdapterCallBack{
		public void onAvatorClick(String userNo);
		public void onLoveClick(Community community,int position,TextView loveView);
		public void onCommunityClick(int communityId,int position);
		public void onItemClick(int communityId,int position);
		public void onShareClick(int communityId,int position);
		public void onDeleteClick(int communityId,int position);
		public void onPicClick(Community community,int position);
	}
	AdapterCallBack mAdapterCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mAdapterCallBack = actionCallBack;
	}
}
